#!/usr/bin/python3
# coding=utf-8
"""
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: YangYunYi
# Create: 2020-11-04
"""
import os
import autoimport.logmodule.logger as logger
import autoimport.getdependinfo.utils as utils
import autoimport.getdependinfo.query_depend as QueryDepend
import autoimport.getdependinfo.filter_branch as FilterBranch
import autoimport.getdependinfo.sort_depend as SortDepend

CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
OUTPUT_PATH = "output"
DOWNLOAD_PATH = "download"
TEMP_PATH = "temp"
INSTALL_LINE_STEP = 4
BUILD_LINE_STEP = 3

this_logger = logger.init('getrequires')
logger.add_file_handler(this_logger, "getrequires", "getrequires")


def init_workspace():
    """
    初始化工作目录
    :return:无
    """
    dirs = [DOWNLOAD_PATH, TEMP_PATH, OUTPUT_PATH]
    for init_dir in dirs:
        # 初始化路径
        dir_path = os.path.join(CURRENT_PATH, init_dir)
        if os.path.exists(dir_path):
            return 
        this_logger.info("init_workspace dir path:%s", dir_path)
        os.makedirs(dir_path, exist_ok=True)


def main():
    """
    主函数
    :return: 无
    """
    # 初始化工作目录
    init_workspace()
    # 获取输入的所有包
    data_path = utils.get_file_path('', 'input_package_list.txt')
    input_pkg = utils.get_input_pkg_list(data_path)
    # 查询自编译依赖
    QueryDepend.get_selfdepend_csv(input_pkg)
    # 依赖结果排序
    SortDepend.main(input_pkg)
    # 识别依赖包是否已经存在，以及所在分支
    FilterBranch.main()


if __name__ == '__main__':
    main()
